package com.wc.alorithm_blue_bridge._动态规划.线性DP.对局匹配;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/1/14 10:34
 * @description 对局匹配
 * https://www.lanqiao.cn/problems/107/learning/?page=1&first_category_id=1
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int maxN = (int) 1e5 + 7;
    static int[] dp = new int[maxN];
    static int[] map = new int[maxN];
    static int[] nums = new int[maxN];
    static int numLen = 0;
    static int n, k;

    public static void main(String[] args) {
        n = sc.nextInt();
        k = sc.nextInt();
        int maxNum = 0;
        for (int i = 1; i <= n; i++) {
            int num = sc.nextInt();
            if (map[num] == 0) {
                nums[++numLen] = num;
            }
            maxNum = Math.max(maxNum, num);
            map[num]++;
        }
        Arrays.sort(nums, 1, numLen + 1);
        if (k == 0) {
            System.out.println(numLen);
            return;
        }
        int ans = 0;
        for (int i = 0; i < k; i++) {
            dp[i] = map[i];
            dp[i + k] = Math.max(dp[i], map[i + k]);
            int j = i + 2 * k;
            for (; j <= maxNum; j += k) {
                dp[j] = Math.max(dp[j - k], dp[j - 2 * k] + map[j]);
            }
            ans += dp[j - k];
        }

        out.println(ans);
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }
}
